package com.alinesno.cloud.gateway.admin.utils;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.google.gson.Gson;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Http请求工具对象
 * 
 * @author WeiXiaoJin
 * @since 2020年2月1日 下午19:45:08
 */
public final class ConstomOkHttpClient {

	/**
	 * The constant JSON.
	 */
	private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

	private static final ConstomOkHttpClient OK_HTTP_TOOLS = new ConstomOkHttpClient();

	private static final Gson GOSN = new Gson();

	private OkHttpClient client;

	private ConstomOkHttpClient() {
		OkHttpClient.Builder builder = new OkHttpClient.Builder();
		builder.connectTimeout(10, TimeUnit.SECONDS);
		builder.readTimeout(10, TimeUnit.SECONDS);
		builder.writeTimeout(10, TimeUnit.SECONDS);
		client = builder.build();
	}

	/**
	 * Gets instance.
	 *
	 * @return the instance
	 */
	public static ConstomOkHttpClient getInstance() {
		return OK_HTTP_TOOLS;
	}

	/**
	 * Post string.
	 *
	 * @param url  the url
	 * @param json the json
	 * @return the string
	 * @throws IOException the io exception
	 */
	public String post(final String url, final String json) throws IOException {
		RequestBody body = RequestBody.create(JSON, json);
		Request request = new Request.Builder().url(url).post(body).build();
		return client.newCall(request).execute().body().string();

	}

	/**
	 * Post t.
	 *
	 * @param <T>      the type parameter
	 * @param url      the url
	 * @param json     the json
	 * @param classOfT the class of t
	 * @return the t
	 * @throws IOException the io exception
	 */
	public <T> T post(final String url, final String json, final Class<T> classOfT) throws IOException {
		RequestBody body = RequestBody.create(JSON, json);
		Request request = new Request.Builder().url(url).post(body).build();
		Response response = client.newCall(request).execute();
		assert response.body() != null;
		final String result = response.body().string();
		return GOSN.fromJson(result, classOfT);

	}

	/**
	 * Post string.
	 *
	 * @param url    the url
	 * @param params the params
	 * @return the string
	 * @throws IOException the io exception
	 */
	public String post(final String url, final Map<String, Object> params) throws IOException {
		String json = GOSN.toJson(params);
		RequestBody body = RequestBody.create(JSON, json);
		Request request = new Request.Builder().url(url).post(body).build();
		return client.newCall(request).execute().body().string();
	}

	/**
	 * get 请求
	 * @param url
	 * @return 
	 * @throws IOException
	 */
	public String get(String url) throws IOException {
		OkHttpClient client = new OkHttpClient();
		Request request = new Request.Builder().url(url).build();
		try (Response response = client.newCall(request).execute()) {
			String responseBody = response.body().string();
			return responseBody ; 
		}
	}

	/**
	 * Gets gosn.
	 *
	 * @return the gosn
	 */
	public Gson getGosn() {
		return GOSN;
	}

}
